package com.szsfm.vehiclemanagement.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.szsfm.vehiclemanagement.R;
import com.szsfm.vehiclemanagement.adapter.TabFragmentAdapter;
import com.szsfm.vehiclemanagement.base.BaseActivity;
import com.szsfm.vehiclemanagement.bean.User;
import com.szsfm.vehiclemanagement.bean.UserInfo;
import com.szsfm.vehiclemanagement.service.RecogCarNumberThread;
import com.szsfm.vehiclemanagement.tcp.TcpCore;
import com.szsfm.vehiclemanagement.tcp.TcpUtil;
import com.szsfm.vehiclemanagement.util.DeviceUtil;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;
    private TabFragmentAdapter mFragmentAdapter;
    private RadioGroup mRadioGroup;
    private RadioButton mHomeButton;
    private TextView mTitleTextView;

    private OnActivityResultListener onActivityResultListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitleTextView = (TextView) findViewById(R.id.title_text_view);
        mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
        mRadioGroup = (RadioGroup) findViewById(R.id.main_radio_group);
        mHomeButton = findRadioButton(R.id.radio_button_home);
        setRadioButtonDrawableBounds();
        mFragmentAdapter = new TabFragmentAdapter(this);
        mViewPager.setAdapter(mFragmentAdapter);
        mViewPager.addOnPageChangeListener(this);
        mRadioGroup.setOnCheckedChangeListener(this);
        mViewPager.setOffscreenPageLimit(4);
        if (!UserInfo.shareInstance().getRoads().isEmpty()){
            mTitleTextView.setText(UserInfo.shareInstance().getRoads().get(0));
        } else{
            mTitleTextView.setText(mHomeButton.getText().toString());
        }

    }


    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            int item = mViewPager.getCurrentItem();
            if (item==0&& !UserInfo.shareInstance().getRoads().isEmpty()){
                mTitleTextView.setText(UserInfo.shareInstance().getRoads().get(0));
            }else {
                RadioButton rb = mRadioGroup.findViewWithTag(item + "");
                mTitleTextView.setText(rb.getText().toString());
            }

        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton button=radioGroup.findViewById(i);
        int tag =Integer.parseInt((String) button.getTag());
        mViewPager.setCurrentItem(tag);
    }

    //提供各页面跳转
    public static void startSelf(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    private RadioButton findRadioButton(int id) {
        return mRadioGroup.findViewById(id);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String Path = "";
        if (resultCode == RESULT_OK) {
            Path = data.getStringExtra("Path");
            RecogCarNumberThread recogCarNumberThread = new RecogCarNumberThread(MainActivity.this, handler, Path, true);
            recogCarNumberThread.start();
        }

        if (data != null && onActivityResultListener != null){
            onActivityResultListener.onActivityResult(requestCode, resultCode, data,Path);
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String CarColor = bundle.getString("CarColor");
            String CarNumber = bundle.getString("CarNumber");
            if (onActivityResultListener!=null&& !TextUtils.isEmpty(CarNumber)){
                onActivityResultListener.onPlateNoResult(CarNumber);
            }
        }
    };

    public void setActivityResultListener(OnActivityResultListener listener) {
        this.onActivityResultListener = listener;
    }

    public interface OnActivityResultListener {
        void onActivityResult(int requestCode, int resultCode, Intent data,String path);
        void onPlateNoResult(String CarNumber);
    }

    //重新设置radiobutton的drawable大小和位置
    private void setRadioButtonDrawableBounds() {
        int defWidth = DeviceUtil.dip2px(this,15);
        int defHeight = DeviceUtil.dip2px(this,15);
        int[] drawables = {R.drawable.ic_home, R.drawable.ic_vehicle, R.drawable.ic_msg, R.drawable.ic_setting};
        for (int i = 0; i < drawables.length; i++) {
            Drawable drawableHome = getResources().getDrawable(drawables[i]);
            drawableHome.setBounds(0, 0, defWidth, defHeight);//第一0是距左右边距离，第二0是距上下边距离，第三69长度,第四宽度
            ((RadioButton) mRadioGroup.getChildAt(i)).setCompoundDrawables(null, drawableHome, null, null);//只放上面
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        User.shareInstance().setUserID(null);
        TcpCore.enableConn = false;
        TcpUtil.stopSavService(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
